{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Nim Magic\n", "\n", "This notebook explores making a cell magic for nim." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, we load libs that we will need for defining an IPython magic:" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "collapsed": true }, "outputs": [], "source": [ "from IPython.core.magic import register_line_cell_magic" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "Now, we construct a file that contains nim code:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Overwriting default.nim\n" ] } ], "source": [ "%%file default.nim\n", "\n", "# This is a comment\n", "echo(\"What's your name? \")\n", "var name: string = \"Kevin\"\n", "echo(\"Hi, \", name, \"!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Can we run the file and view the results?" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "What's your name? \n", "Hi, Kevin!\n", "/etc/nim.cfg(53, 2) Hint: added path: '/home/dblank/.babel/pkgs/' [Path]\n", "/etc/nim.cfg(54, 2) Hint: added path: '/home/dblank/.nimble/pkgs/' [Path]\n", "Hint: used config file '/etc/nim.cfg' [Conf]\n", "Hint: system [Processing]\n", "Hint: default [Processing]\n", "[Linking]\n", "Hint: operation successful (9000 lines compiled; 0.144 sec total; 14.148MB; Debug Build) [SuccessX]\n", "/home/dblank/default \n", "\n" ] } ], "source": [ "import subprocess\n", "import sys\n", "p = subprocess.Popen([\"nim\", \"compile\", \"--run\", \"default.nim\"], stdout=subprocess.PIPE)\n", "out, err = p.communicate()\n", "if err:\n", " print(err.decode(\"utf-8\"), file=sys.stderr)\n", "if out:\n", " print(out.decode(\"utf-8\"))" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "Now, we put that together into a cell magic:" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "collapsed": false }, "outputs": [], "source": [ "@register_line_cell_magic\n", "def nim(line, cell):\n", " fp = open(\"default.nim\", \"w\")\n", " fp.write(cell)\n", " fp.close()\n", " p = subprocess.Popen([\"nim\", \"compile\", \"--run\", \"default.nim\"], stdout=subprocess.PIPE)\n", " out, err = p.communicate()\n", " if err:\n", " print(err.decode(\"utf-8\"), file=sys.stderr)\n", " if out:\n", " print(out.decode(\"utf-8\")) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And now test it out:" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "woof\n", "70\n", "meow\n", "10\n", "/etc/nim.cfg(53, 2) Hint: added path: '/home/dblank/.babel/pkgs/' [Path]\n", "/etc/nim.cfg(54, 2) Hint: added path: '/home/dblank/.nimble/pkgs/' [Path]\n", "Hint: used config file '/etc/nim.cfg' [Conf]\n", "Hint: system [Processing]\n", "Hint: default [Processing]\n", "CC: default\n", "CC: system\n", "[Linking]\n", "Hint: operation successful (9017 lines compiled; 0.791 sec total; 14.148MB; Debug Build) [SuccessX]\n", "/home/dblank/default \n", "\n" ] } ], "source": [ "%%nim \n", "\n", "type Animal = ref object of RootObj\n", " name: string\n", " age: int\n", "method vocalize(this: Animal): string = \"...\"\n", "method ageHumanYrs(this: Animal): int = this.age\n", "\n", "type Dog = ref object of Animal\n", "method vocalize(this: Dog): string = \"woof\"\n", "method ageHumanYrs(this: Dog): int = this.age * 7\n", "\n", "type Cat = ref object of Animal\n", "method vocalize(this: Cat): string = \"meow\"\n", "\n", "\n", "var animals: seq[Animal] = @[]\n", "animals.add(Dog(name: \"Sparky\", age: 10))\n", "animals.add(Cat(name: \"Mitten\", age: 10))\n", "\n", "for a in animals:\n", " echo a.vocalize()\n", " echo a.ageHumanYrs()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.4.0" } }, "nbformat": 4, "nbformat_minor": 0 }